'''
Created on Aug 1, 2010

@author: behrooz
'''
from amscms.ams.models import Advertizer, Ad
from amscms.ams.widgets import AdvertizerSelectWidgetWrapper
from amscms.core.widgets.widgets import DateTimeWidget, SuggestWidget
from django.forms.fields import DateField, CharField
from django.forms.models import ModelForm, ModelChoiceField
from django.utils.translation import ugettext as _

"""
Ad Form 
"""
class AdForm(ModelForm):
    start_date = DateField(widget=DateTimeWidget(), label=_("Publish Start Date"))
    end_date = DateField(widget=DateTimeWidget(), label=_("Publish End Date"))
    tags = CharField(required=False, widget=SuggestWidget({'url' : '/dict/suggest'}, attrs={'size' : '64'}), label=_("Tags"))
    advertizer = ModelChoiceField(Advertizer.objects.none(), cache_choices=False, required=True, widget=AdvertizerSelectWidgetWrapper(return_url='/ads/register/'), label=_("Advertizer"))
    
    def __init__(self, current_user, *args, **kwargs):
        super(AdForm, self).__init__(*args, **kwargs)
        self.fields['advertizer'].queryset = Advertizer.objects.filter(user=current_user)
        ad = kwargs.get("instance", None)
        if ad is not None:
            tags = ", ".join([t.term.term for t in ad.adtag_set.all()])
            self.fields["tags"].widget.attrs["value"] = tags
         
    class Meta:
        model = Ad
        exclude = ('stats', 'enabled', )

"""
Advertizer Form
"""        
class AdvertizerForm(ModelForm):
    class Meta:
        model = Advertizer
        exclude = ['user']

